﻿@model EAM.Model.User_Dept
@{
    ViewBag.Title = "部门管理";
}
@if (false)
{
    <script src="../../Scripts/jquery-1.5.1-vsdoc.js" type="text/javascript"></script>
    <script src="../../Scripts/ligerUI/js/ligerui.all.js" type="text/javascript"></script>
}
<div id="layout">
    <div id="toolbar" style="padding: 5px;">
        <input type="button" value="增加下级部门" class="btn_green" onclick="addNode()" />
        <input type="button" value="删除部门" class="btn_red" onclick="deleteNode()" />
    </div>
    <table width="98%">
        <tr>
            <td width="240" valign="top">
                <div style="border: 1px solid #ccc; margin: 5px; width: 230px;">
                    <ul id="maintree">
                    </ul>
                </div>
            </td>
            <td valign="top" style="padding-top: 10px;">
                <div id="mainForm">
                    @using (Ajax.BeginForm("edit", new AjaxOptions { HttpMethod = "Post", OnSuccess = "iSuccess" }))
                    {
                        <table>
                            <tr>
                                <td class="editor-label">@Html.LabelFor(m => m.Name)</td>
                                <td>
                                    @Html.TextBoxFor(m => m.Name)
                                    @Html.HiddenFor(m => m.ParentId)
                                </td>
                                <td>@Html.ValidationMessageFor(m => m.Name)</td>
                            </tr>
                            <tr>
                                <td class="editor-label">@Html.LabelFor(m => m.Manager)</td>
                                <td>@Html.TextBoxFor(m => m.Manager) </td>
                                <td>@Html.ValidationMessageFor(m => m.Manager)</td>
                            </tr>
                            <tr>
                                <td class="editor-label">@Html.LabelFor(m => m.OrderIndex)</td>
                                <td>@Html.TextBoxFor(m => m.OrderIndex)</td>
                                <td>@Html.ValidationMessageFor(m => m.OrderIndex)</td>
                            </tr>
                            <tr>
                                <td></td>
                                <td colspan="2">
                                    <input type="submit" value="保存" class="btn_green" />
                                    <input type="reset" value="重置" class="btn_orange" />
                                </td>
                            </tr>
                        </table>
                    }
                </div>
            </td>
        </tr>
    </table>
</div>
<script type="text/javascript">
    var tree = null, node = null;

    $(function () {
        tree = $("#maintree").ligerTree({
            checkbox: false,
            url: '/dept/root?t=' + Math.random(),
            onSelect: editNode
        });
    });

    function iSuccess() {
        tree.clear();
        tree.loadData(null, '/dept/root?t=' + Math.random());

        $("#mainForm").hide();
    }

    function addNode() {
        $("#mainForm").show();
        $("#mainForm :text, #mainForm :hidden").val('');
        $("#mainForm form").attr("action", "/dept/edit");

        node = tree.getSelected();
        if (node) {
            $("#ParentId").val(node.data.id);
        }
    }

    function editNode() {
        node = tree.getSelected();
        if (node) {
            if (node.data.id == 0) {
                $("#mainForm").hide();
                return;
            }

            $("#mainForm").show();
            $("#mainForm form").attr("action", "/dept/edit/" + node.data.id);
            $.getJSON("/dept/edit/" + node.data.id + "?t=" + Math.random(), function (data) {
                $("#Name").val(data.Name);
                $("#ParentId").val(data.ParentId);
                $("#Manager").val(data.Manager);
                $("#OrderIndex").val(data.OrderIndex);
            });
        }
        else {
            alert("请选择节点");
        }
    }

    function deleteNode() {
        if (!confirm("如选中节点存在子节点，则子节点也一并被删除，请慎重操作"))
            return;

        node = tree.getSelected();
        if (node) {
            $.post("/dept/delete/" + node.data.id, function (data) {
                tree.clear();
                tree.loadData(null, '/dept/root?t=' + Math.random());

                $("#mainForm").hide();
            });
        }
        else {
            alert("请选择节点");
        }
    }
</script>
